<template>
  <div class="login-container">
    <div class="login-card neu-card">
      <div class="login-header">
        <h2>图书管理系统</h2>
        <p>请登录您的账号</p>
      </div>
      
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            type="text" 
            id="username" 
            v-model="loginForm.username" 
            class="neu-input" 
            placeholder="请输入用户名"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="loginForm.password" 
            class="neu-input" 
            placeholder="请输入密码"
            required
          />
        </div>
        
        <div class="form-actions">
          <button type="submit" class="neu-button primary" :disabled="loading">
            {{ loading ? '登录中...' : '登录' }}
          </button>
          <router-link to="/register" class="register-link">
            没有账号？立即注册
          </router-link>
        </div>
      </form>
      
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
      
      <div class="login-tips">
        <p>测试账号：</p>
        <p>管理员：admin / admin123</p>
        <p>普通用户：user / user123</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userService } from '../api/services'
import store from '../store'

const router = useRouter()

// 登录表单数据
const loginForm = ref({
  username: '',
  password: ''
})

// 状态变量
const loading = ref(false)
const errorMessage = ref('')

// 登录处理
const handleLogin = async () => {
  try {
    loading.value = true
    errorMessage.value = ''
    
    const response = await userService.login(loginForm.value)
    
    if (response.code === 200) {
      // 登录成功，保存用户信息和token
      store.setUserInfo(response.data.user, response.data.token)
      
      // 跳转到首页
      router.push('/dashboard')
    } else {
      errorMessage.value = response.message || '登录失败'
    }
  } catch (error) {
    console.error('登录错误:', error)
    errorMessage.value = '登录失败，请稍后再试'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
@import '../assets/styles/neumorphism.css';

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  background-color: var(--bg-color);
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: var(--primary-green);
  margin-bottom: 10px;
}

.login-header p {
  color: var(--text-secondary);
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-weight: 500;
  color: var(--text-secondary);
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-top: 10px;
}

.register-link {
  text-align: center;
  color: var(--primary-blue);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
}

.register-link:hover {
  color: var(--primary-blue-dark);
}

.error-message {
  margin-top: 20px;
  padding: 10px;
  border-radius: 8px;
  background-color: rgba(244, 67, 54, 0.1);
  color: var(--danger);
  text-align: center;
}

.login-tips {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid var(--dark-shadow);
  font-size: 14px;
  color: var(--text-light);
}

.login-tips p {
  margin-bottom: 5px;
}
</style>